﻿using KTD.Web.BLL;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XXS.Common;
using XXS.Common.ControllerExt;

namespace KTD.Web.Controllers
{
    public class AreaController : Controller
    {
        // GET: Area
        public ActionResult Index(AreaQD im)
        {
            ViewBag.isSon = im.isSon;
            var cityLit = MF.AreaManager.GetSelect().ToList();
            cityLit.Insert(0, new SelectListItem() { Value = "", Text = "全部市州" });
            ViewBag.CityList = cityLit;
            var res = MF.AreaManager.GetListBy(im);
            res.ControllerContext = ControllerContext;
            return View(res);
        }

        public ActionResult Detail(int Id)
        {

            ViewBag.Id = Id;
            var res = MF.AreaManager.Find(Id);
            return View(res);
        }


        [HttpPost]
        public ActionResult Create(AreaCUD im)
        {
           
            MF.AreaManager.Create(im);
            return this.JsonNet(new { succuess = true });
        }

        [HttpGet]
        public ActionResult Create(string isSon)
        {
            ViewBag.CityList = MF.AreaManager.GetSelect();
            ViewBag.isSon = isSon;
            var model = new AreaCUD();
            return View(model);
        }



        [HttpGet]
        public ActionResult Update(int Id,string isSon)
        {
            ViewBag.isSon = isSon;
            ViewBag.CityList = MF.AreaManager.GetSelect();
            var model = MF.AreaManager.Find(Id);
            return View(model);
        }
      

        public ActionResult Delete(int Id)
        {
            try
            {
                MF.AreaManager.Delete(Id);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message });
            }
        }


        public ActionResult Find(int Id)
        {
            try
            {
                var res = MF.AreaManager.Find(Id);
                return this.JsonNet(new { success = true, data = res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult FindByName(string areaName)
        {
            try
            {
                var res = MF.AreaManager.FindByName(areaName);
                return this.JsonNet(new { success = true, data = res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public ActionResult Update(AreaCUD im)
        {
            try
            {
                MF.AreaManager.Update(im);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message });
            }
        }
        [OutputCache(Duration = 60 * 60 * 24 * 2, VaryByParam = "*")]
        public ActionResult GetList(AreaQD im)
        {
            try
            {
                im.PageSize = 100;
                var res = MF.AreaManager.GetListBy(im);
                return this.JsonNet(res.PageData.ToExtGrid(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [OutputCache(Duration = 60 * 60 * 24 * 10, VaryByParam = "*")]
        public ActionResult GetCityList()
        {
            try
            {
                var res = MF.AreaManager.GetCityListBy();
                return this.JsonNet(res.ToExtGrid(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}